package com.wrupple.muba.catalogs.client.widgets.fields.cells;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.TakesValue;
import com.google.gwt.user.client.ui.Composite;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.shared.State.StorageManager;
import com.wrupple.muba.common.shared.StateTransition;

public abstract class AbstractForeignKeyViewer<V,T extends JavaScriptObject> extends Composite implements TakesValue<V>{
	
	class EntryCallback extends DataCallback<T>{

		@Override
		public void execute() {
			setRealValue(result);
		}
		
	}
	protected StorageManager sm;
	
	private String key ;
	private V value;
	private String type;
	private StateTransition<T> callback;

	public AbstractForeignKeyViewer(StorageManager sm, String type) {
		super();
		this.sm = sm;
		this.type = type;
		this.callback = new EntryCallback();
	}


	protected abstract void setRealValue(T result);


	@Override
	public void setValue(V value) {
		this.value=value;
		if(value==null){
			setRealValue(null);
		}else{
			key = value.toString();
			sm.read(type, key, callback);
		}
	}


	@Override
	public V getValue() {
		return value;
	}



	

}
